﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SD.IDAL;
using System.Data.SqlClient;
using System.Data;
using SD.Entities;

namespace SD.DAL
{
    public class ServiceDAO:BaseDAO, IServiceDAO
    {
        #region IServiceDAO Members

        public List<Service> GetAll(Boolean includeInactive)
        {
            try
            {
                SqlCommand comm = CreateCommand();
                comm.CommandText = "ServiceGetAll";
                comm.Parameters.AddWithValue("@includeInactive", includeInactive);

                DataTable dt = ExecuteSelectCommand(comm);
                List<Service> list = new List<Service>();
                Service s = null;
                foreach (DataRow row in dt.Rows)
                {
                    s = new Service();
                    s.ServiceId = row["ServiceId"].ToString();
                    s.Name = row["Name"].ToString();
                    list.Add(s);
                }
                return list;
            }
            catch (SqlException ex)
            {

                throw ex;
            }
        }

        public DataTable GetAllInfo(Boolean includeInactive)
        {
            throw new NotImplementedException();
        }

        public DataTable GetAllInfoByFilter(String filterExpression, Boolean includeInactive)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// Get Service by its ID
        /// </summary>
        /// <param name="serviceId">ID of the service</param>
        /// <returns>Found service</returns>
        public Service GetById(string serviceId)
        {
            Service resultService = null;

            SqlCommand comm = CreateCommand();
            comm.CommandText = "ServiceGetById";
            comm.Parameters.AddWithValue("@serviceId", serviceId);

            DataTable dtb;
            try
            {
                dtb = ExecuteSelectCommand(comm);
            }
            catch (SqlException ex)
            {
                throw ex;
            }

            if (dtb != null && dtb.Rows.Count > 0)
            {
                #region Set Details
                resultService = new Service();
                resultService.ServiceId = dtb.Rows[0]["ServiceID"].ToString();
                resultService.Name = dtb.Rows[0]["Name"].ToString();
                resultService.ShortDescription = dtb.Rows[0]["ShortDescription"].ToString();
                resultService.ClientDescription = dtb.Rows[0]["ClientDescription"].ToString();
                resultService.ServiceAttendance = dtb.Rows[0]["ServiceAttendance"].ToString();
                resultService.StartExpected = Convert.ToDateTime(dtb.Rows[0]["StartExpected"]);
                resultService.StartDate = Convert.ToDateTime(dtb.Rows[0]["StartDate"]);
                resultService.EndDate = Convert.ToDateTime(dtb.Rows[0]["EndDate"]);
                resultService.IsExtendable = Convert.ToBoolean(dtb.Rows[0]["IsExtendable"]);
                resultService.ExtendableMonth = Convert.ToInt32(dtb.Rows[0]["ExtendableMonth"]);
                resultService.ExtendableYear = Convert.ToInt32(dtb.Rows[0]["ExtendableYear"]);
                resultService.FullDescription = dtb.Rows[0]["FullDescription"].ToString();
                resultService.DebtCode = dtb.Rows[0]["DebtCode"].ToString();
                resultService.DescriptionDelivery = dtb.Rows[0]["DescriptionDelivery"].ToString();
                resultService.ServiceContractCode = dtb.Rows[0]["ServiceContractCode"].ToString();
                resultService.ServiceContractValue = dtb.Rows[0]["ServiceContractValue"].ToString();
                resultService.ContactStagedPayment = Convert.ToBoolean(dtb.Rows[0]["ContactStagedPayment"]);
                resultService.IsTimeLimited = Convert.ToBoolean(dtb.Rows[0]["IsTimeLimited"]);
                resultService.TimeLimitedMonth = Convert.ToInt32(dtb.Rows[0]["TimeLimitedMonth"]);
                resultService.TimeLimitedYear = Convert.ToInt32(dtb.Rows[0]["TimeLimitedYear"]);
                resultService.Participation = dtb.Rows[0]["Participation"].ToString();
                resultService.LeadContact = dtb.Rows[0]["LeadContactID"] == DBNull.Value ? null : new ContactDAO().GetById(dtb.Rows[0]["LeadContactID"].ToString());
                resultService.SubType = dtb.Rows[0]["SubType"] == DBNull.Value ? null : new ReferenceDataDAO().GetById(dtb.Rows[0]["SubType"].ToString());
                resultService.ServiceType = dtb.Rows[0]["ServiceType"] == DBNull.Value ? null : new ReferenceDataDAO().GetById(dtb.Rows[0]["ServiceType"].ToString());
                resultService.RefferalProcess = dtb.Rows[0]["RefferalProcess"] == DBNull.Value ? null : new ReferenceDataDAO().GetById(dtb.Rows[0]["RefferalProcess"].ToString());
                resultService.Programme = dtb.Rows[0]["ProgrammeID"] == DBNull.Value ? null : new ProgrammeDAO().GetById(dtb.Rows[0]["ProgrammeID"].ToString());
                resultService.IsActive = Convert.ToBoolean(dtb.Rows[0]["IsActive"].ToString());
                #endregion
            }

            return resultService;
        }

        public Boolean Insert(SD.Entities.Service service)
        {
            throw new NotImplementedException();
        }

        public Boolean Update(SD.Entities.Service service)
        {
            throw new NotImplementedException();
        }

        public Boolean SetActive(string serviceId, bool isActive)
        {
            throw new NotImplementedException();
        }

        public Boolean IsDuplicateName(String checkedName)
        {
            throw new NotImplementedException();
        }

        #endregion
    }
}
